package gui.specialGUIs;

import gui.designComponents.TMTPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import util.Statistic;
import util.StringStringPair;
import app.Controller;
import app.Model;
import bundles.BundleLoader;
import definitions.Constants;

/**
 * Panel showing Statistic
 * 
 * @author MK
 * 
 */
public class StatsPanel extends TMTPanel {

	private StatsOverviewButtonPanel buttonPanel;
	private StatsDrawingPanel statsDrawingPanel;

	public StatsPanel(Model model, Controller controller) {

		setLayout(new BorderLayout());

		this.buttonPanel = new StatsOverviewButtonPanel(model, controller);
		this.statsDrawingPanel = new StatsDrawingPanel(model);

		add(buttonPanel, BorderLayout.NORTH);
		add(statsDrawingPanel, BorderLayout.CENTER);
	}

	public void update() {
		StringStringPair ssp = (StringStringPair) buttonPanel
				.getStatsSelection().getSelectedItem();
		String whatIsShown = ssp.getObjValue();
		statsDrawingPanel.update(whatIsShown);
		statsDrawingPanel.repaint();
	}

	public void printStat() {
		statsDrawingPanel.printStat();
	}
}

class StatsDrawingPanel extends TMTPanel implements Printable {

	private Model model;
	private Statistic stat;
	private double maxYValue;

	public StatsDrawingPanel(Model model) {
		this.model = model;
	}

	public void update(String whatIsShown) {

		if (whatIsShown.equals(Constants.STAT_ATTENDANCE_OVERVIEW)) {
			stat = model.getAttendanceStat();
			maxYValue = stat.getHighestYValue();

		} else if (whatIsShown.equals(Constants.STAT_RATING_AVERAGE_OVERVIEW)) {
			maxYValue = 6;
			stat = model.getRatingAverageStat();
		}
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		drawPage(g2d, "paint");
	}

	public void drawPage(Graphics2D g2d, String action) {
		int width = getWidth() - 150;
		int height = getHeight() - 250;
		int xPadding = 80;
		int yPadding = 100;
		int counter = 0;
		int xSpace = 80;
		int ySpace = 135;
		int yLength = stat.getyValues().length - 1;

		AffineTransform af = new AffineTransform();

		if (action.equals("print")) {
			af.scale(5.0, 5.0);
			g2d.setTransform(af);
		}
		
		g2d.setColor(Color.BLACK);
		
		for (double value : stat.getyValues()) {
			af.rotate(Math.toRadians(90), (width / yLength) * counter
					+ xPadding - 2, height + yPadding + 20);
			g2d.setTransform(af);
			g2d
					.drawString(stat.getxValues()[counter], (width / yLength)
							* counter + xPadding - 2 + 135, height + yPadding
							+ 20 - 80);
			af.rotate(-Math.toRadians(90), (width / yLength) * counter
					+ xPadding - 2, height + yPadding + 20);
			g2d.setTransform(af);
			g2d.setColor(Color.RED);
			g2d.drawRect((width / yLength) * counter + xPadding + xSpace,
					height + yPadding + ySpace, 10, -height
							* (int) (value * 100) / (int) (maxYValue * 100));
			g2d.setColor(Color.BLACK);
			g2d
					.drawString(
							"" + (double) (Math.round(value * 100)) / 100,
							(width / yLength) * counter + xPadding - 5 + xSpace,
							height
									+ yPadding
									- (height * (int) (value * 100) / (int) (maxYValue * 100))
									- 20 + ySpace);
			counter++;
		}

		g2d.drawLine(50 + xSpace, height + yPadding + ySpace, width + xPadding
				+ xSpace + 10, height + yPadding + ySpace);
		g2d.drawLine(50 + xSpace, height + yPadding + ySpace, 50 + xSpace,
				height + yPadding - height + ySpace);

		for (double i = 0; i <= maxYValue; i++) {
			g2d.drawString("" + i, 20 + xSpace,
					(int) (height + yPadding - (height / maxYValue) * i)
							+ ySpace);
		}
	}

	public void printStat() {
		PrinterJob job = PrinterJob.getPrinterJob();
		job.setPrintable(this);
		boolean ok = job.printDialog();
		if (ok) {
			try {
				job.print();
			} catch (PrinterException ex) {
				
				ResourceBundle resBundle = BundleLoader.getResourceBundle();
				
				String messageContent = resBundle.getString("failurePrint");
				String messageTitle = resBundle.getString("attention");

				JOptionPane.showMessageDialog(null, messageContent, messageTitle,
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public int print(Graphics g, PageFormat pf, int page)
			throws PrinterException {
		Graphics2D g2d = (Graphics2D) g;
		if (page > 0) {
			return NO_SUCH_PAGE;
		}
		drawPage(g2d, "print");
		return PAGE_EXISTS;
	}
}